package handler

import (
	"boy-go/middlewares"
	"boy-go/modules/demo/service"
	"boy-go/modules/demo/vo"
	"boy-go/pkg/request"
	"boy-go/pkg/response"
	"boy-go/pkg/xexcel"
	"boy-go/pkg/xnum"
	"github.com/gin-gonic/gin"
	"github.com/jinzhu/copier"
	"strings"
)

// AddTCompanyRouter 添加路由
func AddTCompanyRouter(group *gin.RouterGroup) {
	group.GET("/t_company/:id", middlewares.HasPermission("demo:t_company:query"), get_t_company)
	group.GET("/t_company/list", middlewares.HasPermission("demo:t_company:list"), list_t_company)
	group.POST("/t_company", middlewares.HasPermission("demo:t_company:add"), middlewares.Logger("演示公司表", middlewares.INSERT), add_t_company)
	group.PUT("/t_company", middlewares.HasPermission("demo:t_company:edit"), middlewares.Logger("演示公司表", middlewares.UPDATE), edit_t_company)
	group.POST("/t_company/export", middlewares.HasPermission("demo:t_company:export"), middlewares.Logger("演示公司表", middlewares.EXPORT), export_t_company)
	group.DELETE("/t_company/:ids", middlewares.HasPermission("demo:t_company:remove"), middlewares.Logger("演示公司表", middlewares.DELETE), del_t_company)
}

// get_t_company
// @Summary 获取演示公司表详情
// @Description 获取演示公司表详情（管理页面）
// @Tags demo-t_company
// @Accept json
// @Produce json
// @Param id path string true "ID"
// @Success 200 {object} response.R[vo.TCompanyModel] "ok"
// @Failure 400 {string} string "bad request"
// @Failure 500 {string} string "Internal Server Error"
// @Router /demo/t_company/{id} [get]
func get_t_company(ctx *gin.Context) {
	var idStr = ctx.Param("id")
	id := xnum.Str2int64(idStr)
	m, err := service.NewTCompanyService().GetById(id)
	if err != nil {
		response.AjaxError(ctx, err)
		return
	}
	response.AjaxSuccess(ctx, m)
}

// list_t_company
// @Summary 获取演示公司表列表
// @Description 获取演示公司表列表（管理页面）
// @Tags demo-t_company
// @Accept json
// @Produce json
// @Success 200 {object} response.R[PageRes[vo.TCompanyModel]] "ok"
// @Failure 400 {string} string "bad request"
// @Failure 500 {string} string "Internal Server Error"
// @Router /demo/t_company/list [get]
func list_t_company(ctx *gin.Context) {
	req := vo.QueryTCompanyReq{}
	if err := ctx.BindQuery(&req); err != nil {
		response.AjaxError(ctx, err)
		return
	}
	req.Params = request.BindFormParams(ctx)
	var pageReq = &request.PageRequest{
		PageNum:  req.PageNum,
		PageSize: req.PageSize,
	}
	page, err := service.NewTCompanyService().List(&req, pageReq)
	if err != nil {
		response.AjaxError(ctx, err)
		return
	}
	response.AjaxDataTable(ctx, page.List, page.Total)
}

// export_t_company
// @Summary 导出演示公司表列表
// @Description 导出演示公司表列表（管理页面）
// @Tags demo-t_company
// @Accept json
// @Produce json
// @Success 200 {object} response.R[PageRes[vo.TCompanyModel]] "ok"
// @Failure 400 {string} string "bad request"
// @Failure 500 {string} string "Internal Server Error"
// @Router /demo/t_company/export [post]
func export_t_company(ctx *gin.Context) {
	req := vo.QueryTCompanyReq{}
	if err := ctx.BindQuery(&req); err != nil {
		response.AjaxError(ctx, err)
		return
	}
	req.Params = request.BindFormParams(ctx)
	page, err := service.NewTCompanyService().List(&req, nil)
	if err != nil {
		response.AjaxError(ctx, err)
		return
	}
	exports := make([]*vo.TCompanyExcelModel, 0)
	for _, m := range page.List {
		xv := &vo.TCompanyExcelModel{}
		err = copier.Copy(&xv, m)
		if err != nil {
			continue
		}
		exports = append(exports, xv)
	}
	excel := xexcel.NewXExcel[vo.TCompanyExcelModel]("演示公司表")
	excel.Export(ctx, exports)
}

// add_t_company
// @Summary 新增演示公司表
// @Description 新增演示公司表（管理页面）
// @Tags demo-t_company
// @Accept json
// @Produce json
// @Param req body  vo.AddTCompanyReq true "req"
// @Success 200 {object} response.R[vo.AddTCompanyRsp] "ok"
// @Failure 400 {string} string "bad request"
// @Failure 500 {string} string "Internal Server Error"
// @Router /demo/t_company [post]
func add_t_company(ctx *gin.Context) {
	var req vo.AddTCompanyReq
	if err := request.BindJSONParams(ctx, &req); err != nil {
		response.AjaxError(ctx, err)
		return
	}
	m, err := service.NewTCompanyService().Add(&req)
	if err != nil {
		response.AjaxFailCode(ctx, response.ServerError)
	} else {
		response.AjaxSuccess(ctx, m)
	}
}

// edit_t_company
// @Summary 编辑演示公司表
// @Description 编辑演示公司表（管理页面）
// @Tags demo-t_company
// @Accept json
// @Produce json
// @Param req body  vo.EditTCompanyReq true "req"
// @Success 200 {object} response.R[vo.EditTCompanyRsp] "ok"
// @Failure 400 {string} string "bad request"
// @Failure 500 {string} string "Internal Server Error"
// @Router /demo/t_company [put]
func edit_t_company(ctx *gin.Context) {
	var req vo.EditTCompanyReq
	if err := ctx.ShouldBind(&req); err != nil {
		response.AjaxError(ctx, err)
		return
	}
	m, err := service.NewTCompanyService().Edit(&req)
	if err != nil {
		response.AjaxFailCode(ctx, response.ServerError)
	} else {
		response.AjaxSuccess(ctx, m)
	}
}

// del_t_company
// @Summary 删除演示公司表
// @Description 删除演示公司表（管理页面）
// @Tags demo-t_company
// @Accept json
// @Produce json
// @Success 200 {object} response.R[string] "ok"
// @Failure 400 {string} string "bad request"
// @Failure 500 {string} string "Internal Server Error"
// @Router /demo/t_company [delete]
func del_t_company(ctx *gin.Context) {
	var idsStr = ctx.Param("ids")
	ids := strings.Split(idsStr, ",")
	err := service.NewTCompanyService().BatchDel(ids)
	if err != nil {
		response.AjaxFailCode(ctx, response.ServerError)
	} else {
		response.AjaxSuccess(ctx, nil)
	}
}
